package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.courseMapper.CourseMapper;
import com.example.fitnessservice.module.dao.usermapper.UserMapper;
import com.example.fitnessservice.module.pojo.CourseDetails;
import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.module.pojo.CourseOrderDetails;
import com.example.fitnessservice.module.pojo.Place;
import com.example.fitnessservice.module.utils.DistanceUtil;
import com.example.fitnessservice.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/21 11:03
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private CourseMapper courseMapper;

    @Override
    public List<Place> orderDistance(Double x, Double y) {
        //在查询场馆信息，以及场馆下的课程和训练营的时候，先更新训练营的状态
        userMapper.updateCourseStatus2();
        userMapper.updateCourseStatus3();

        //获取到所有场馆的信息
        List<Place> places = userMapper.findAllPlace();

        for (Place place : places) {
            //通过工具类计算两点之间的距离
            double distance = DistanceUtil.getDistance(x, y, place.getLongitude(), place.getLatitude(), 2);
            //将距离和场馆信息都放到map集合中
            place.setDistance(distance);
        }
        //按照距离排序
        places.sort((o1, o2) -> {
            if (o1.getDistance() > o2.getDistance()) {
                return 1;
            } else if (o1.getDistance() == o2.getDistance()) {
                return 0;
            }
            return -1;
        });
        return places;
    }

    @Override
    public List<CourseDetails> findUserCourse(Integer userId, Integer status, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);

        List<CourseDetails> list= userMapper.findUserCourse1(userId, status);

        for (CourseDetails course : list) {
            course.setCourseStatus(courseMapper.getStatusByUserIdAndCourseId(userId,course.getId(), 0));
        }
        return list;

    }

    @Override
    public List<CourseDetailsCamp> findUserCamp(Integer userId, Integer status, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<CourseDetailsCamp> list= userMapper.findUserCamp1(userId, status);

        for (CourseDetailsCamp course : list) {
            course.setCourseStatus(courseMapper.getStatusByUserIdAndCourseId(userId,course.getId(), 1));
        }
        return list;
    }

    @Override
    public List<CourseDetails> findAllCourse(Integer userId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<CourseDetails> list= userMapper.findUserCourse(userId);

        for (CourseDetails course : list) {
            course.setCourseStatus(courseMapper.getStatusByUserIdAndCourseId(userId,course.getId(), 0));
        }
        return list;
    }

    @Override
    public List<CourseDetailsCamp> findAllCamp(Integer userId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<CourseDetailsCamp> list= userMapper.findUserCamp(userId);

        for (CourseDetailsCamp course : list) {
            course.setCourseStatus(courseMapper.getStatusByUserIdAndCourseId(userId,course.getId(), 1));
        }
        return list;
    }

    @Override
    public double findUserBalance(Integer userId) {
        return userMapper.findUserBalance(userId);
    }

    @Override
    public List<CourseOrderDetails> findAllCourseOrder(Integer userId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return userMapper.findAllCourseOrder(userId);
    }

    @Override
    public List<CourseOrderDetails> findCourseOrder(Integer userId, Integer status, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return userMapper.findCourseOrder(userId, status);
    }

    @Override
    public List<CourseOrderDetails> findAllCampOrder(Integer userId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return userMapper.findAllCampOrder(userId);
    }

    @Override
    public List<CourseOrderDetails> findCampOrder(Integer userId, Integer status, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return userMapper.findCampOrder(userId, status);
    }
}
